/* Exercise 3.4 Delayed debouce test uneven on/off times */ /* Created David Miles April 2006 */ /* Updated Ben Rowland Febuary 2014 */ #include // CONFIG1 #pragma config FOSC = HS // Oscillator Selection (HS Oscillator, High-speed crystal/resonator connected between OSC1 and OSC2 pins) #pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled) #pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled) #pragma config MCLRE = ON // MCLR Pin Function Select (MCLR/VPP pin function is MCLR) #pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled) #pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled) #pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled) #pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin) #pragma config IESO = OFF // Internal/External Switchover (Internal/External Switchover mode is disabled) #pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled) // CONFIG2 #pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off) #pragma config VCAPEN = OFF // Voltage Regulator Capacitor Enable (All VCAP pin functionality is disabled) #pragma config PLLEN = OFF // PLL Enable (4x PLL disabled) #pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset) #pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.) #pragma config LVP = OFF // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming) #define _XTAL_FREQ 19660800 // Defines the hardware crystal frequency allowing the delay function to work correctly void delay ( int length ) { int i, j; for ( i = 0 ; i < length ; i++ ) //Loop (length) times { for ( j = 0 ; j < 10 ; j++ ); //Loop 10 times } } unsigned char get_debounced_port ( void ) { unsigned char count = 0 ; //Create local variables unsigned char oldv, newv ; oldv = PORTB & 0x1F; //Store current state of PORTB pin 0 while (count < 20) //Loop while count variable less than 20 { newv = PORTB & 0x1F; //collect current state of PORTB pin 0 if (oldv == newv) //If state matches initial state { count++ ; //Increment the count variable } else { count = 0 ; //Reset the count variable oldv = newv ; //Store current state of PORTB pin 0 } } return oldv ; //If current state and old state match for 20 cycles then return state } unsigned int get_move ( void ) { unsigned int debouncedport ; int i ; for ( i = 0 ; i < 100 ; i++ ) { debouncedport = get_debounced_port(); if (debouncedport != 0x00) //If somethings been pressed, return it { return debouncedport; } delay(300); } return 0x00 ; //If we've got here, nothings been pressed so return 0 } int main (void) { unsigned int move; ANSELA = 0x00; //Set PORTA to Digital Mode - Defaults to analogue mode ANSELB = 0x00; //Set PORTB to Digital Mode - Defaults to analogue mode OPTION_REG = 0xC0; TRISA = 0x00; //Set all of PORTA to be configured as an output TRISB = 0xFF; //Set all of PORTB to be configured as an input while (1) //Loop forever { LATA = 0; move = get_move(); if (move == 0x00) { LATA = 0xFF; } else { LATA = move; } delay(30000); } return 0; }